export TREE_INSTALL_HNP_PATH=${HNP_PUBLIC_PATH}/dog.org/dog_1.0.0

# 创建安装目录
mkdir -p ${TREE_INSTALL_HNP_PATH}/usr/bin
mkdir -p ${TREE_INSTALL_HNP_PATH}/usr/share/man/man1

# 设置 Rust 交叉编译目标
# HarmonyOS 使用 musl libc，目标架构是 aarch64
export CARGO_TARGET_DIR=${PWD}/target
export TARGET=aarch64-unknown-linux-musl

# 配置 cargo 使用 HarmonyOS 工具链
export CC_aarch64_unknown_linux_musl=${CC}
export CXX_aarch64_unknown_linux_musl=${CXX}
export AR_aarch64_unknown_linux_musl=${AR}
export RANLIB_aarch64_unknown_linux_musl=${RANLIB}
export STRIP_aarch64_unknown_linux_musl=${STRIP}

# 设置链接器
export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_MUSL_LINKER=${CC}

# 创建 .cargo 配置目录
mkdir -p .cargo

# 动态生成 .cargo/config.toml
# 从环境变量获取 SDK 路径（从 build.sh 设置的 OHOS_SDK）
SDK_PATH=${OHOS_SDK:-/Users/jianguo/Desktop/ohosdk}
cat > .cargo/config.toml << EOF
[target.aarch64-unknown-linux-musl]
linker = "clang"
rustflags = [
    "-C", "link-arg=--target=aarch64-linux-ohos",
    "-C", "link-arg=--sysroot=${SDK_PATH}/native/sysroot",
    "-C", "link-arg=-fuse-ld=lld",
    "-C", "link-arg=--ld-path=${SDK_PATH}/native/llvm/bin/ld.lld",
    # 禁用 --as-needed 选项（LLD 不支持）
    "-C", "link-arg=-Wl,--no-as-needed",
]
EOF

echo "Created .cargo/config.toml with SDK path: ${SDK_PATH}"

# 清理之前的构建
echo "Cleaning previous build..."
cargo clean 2>/dev/null || true

# 构建 release 版本
# 使用 --no-default-features 禁用默认 features（包括 with_nativetls，它需要 OpenSSL）
# 然后启用 with_idna 和 with_rustls（纯 Rust 实现，不需要系统 OpenSSL）
echo "Building dog with cargo..."
cargo build --release --target ${TARGET} --no-default-features --features with_idna,with_tls,with_https,with_rustls --verbose || {
    echo "Error: Cargo build failed"
    exit 1
}

# 检查构建产物
BINARY_PATH=${CARGO_TARGET_DIR}/${TARGET}/release/dog
if [ ! -f "${BINARY_PATH}" ]; then
    echo "Error: Binary not found at ${BINARY_PATH}"
    exit 1
fi

# 安装二进制文件
echo "Installing dog binary..."
cp ${BINARY_PATH} ${TREE_INSTALL_HNP_PATH}/usr/bin/dog || {
    echo "Error: Failed to copy binary"
    exit 1
}

# 设置可执行权限
chmod +x ${TREE_INSTALL_HNP_PATH}/usr/bin/dog

# 构建并安装 man pages（如果 pandoc 可用）
if command -v pandoc &> /dev/null; then
    echo "Building man pages..."
    pandoc --standalone -f markdown -t man man/dog.1.md > ${TREE_INSTALL_HNP_PATH}/usr/share/man/man1/dog.1 2>/dev/null || true
fi

# 复制 hnp.json
cp hnp.json ${TREE_INSTALL_HNP_PATH}/ || {
    echo "Error: Failed to copy hnp.json"
    exit 1
}

# 打包
echo "Packaging..."
pushd ${TREE_INSTALL_HNP_PATH}/../
    ${HNP_TOOL} pack -i ${TREE_INSTALL_HNP_PATH} -o ${ARCHIVE_PATH}/ || {
        echo "Error: HNP pack failed"
        popd
        exit 1
    }
    tar -zvcf ${ARCHIVE_PATH}/ohos_dog_1.0.0.tar.gz dog_1.0.0/ || {
        echo "Error: Tar packaging failed"
        popd
        exit 1
    }
popd

# 打印构建结果
echo ""
echo "=========================================="
echo "Build completed successfully!"
echo "=========================================="
echo "HNP Package: ${ARCHIVE_PATH}/dog.org_dog_1.0.0.hnp"
echo "Tar Archive: ${ARCHIVE_PATH}/ohos_dog_1.0.0.tar.gz"
echo "Installation Path: ${TREE_INSTALL_HNP_PATH}"
echo "=========================================="
echo ""

